package com.itboye.mengwu.app.ui.mymeng;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.MessageXiTongBean;
import com.itboye.mengwu.data.model.MessageXiTongBean.AMessge;
import com.itboye.mengwu.data.presenter.FenSiPresenter;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

public class OrderMessageActivity extends BaseActivity implements
		OnClickListener, Observer, IXListViewListener {
	MsgAdapter adapter;
	ArrayList<MessageXiTongBean.AMessge> list;
	XListView msgListview;
	ImageView back;
	TextView txt_title;
	FenSiPresenter fenSiPresenter;
	MessageXiTongBean bean;
	String uid;
	String msgType = "2";
	private int pageNo = 1;
	RelativeLayout nodata;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activitymsg;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		txt_title.setText("订单消息");
		msgListview.setXListViewListener(this);
		msgListview.setPullLoadEnable(true);
		msgListview.setPullRefreshEnable(true);
		list = new ArrayList<MessageXiTongBean.AMessge>();
		adapter = new MsgAdapter(this, list);
		msgListview.setAdapter(adapter);
		fenSiPresenter = new FenSiPresenter(this);
		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
		fenSiPresenter.systemMessage(uid, msgType, pageNo);
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub

		ResultEntity entity = handlerError(data);
		bean = new MessageXiTongBean();
		if (entity != null) {
			if (entity.getEventType() == FenSiPresenter.MessageSuccess) {
				bean = (MessageXiTongBean) entity.getData();
				if (pageNo == 1) {
					list.clear();
				}
				list.addAll(bean.getList());
				if (list.size() == 0) {
					nodata.setVisibility(View.VISIBLE);
					msgListview.setVisibility(View.GONE);

				} else {
					msgListview.setVisibility(View.VISIBLE);
					nodata.setVisibility(View.GONE);
				}
				adapter.notifyDataSetChanged();
			} else if (entity.getEventType() == FenSiPresenter.MessageFail) {
				ByAlert.alert(entity.getData() + ">>>");
			}

		}
		stopLoad();
		try {
			/**
			 * 当数据加载出来后dialog则消失
			 */
			closeProgressDialog();
		} catch (Exception e) {

		}
	}

	class MsgAdapter extends BaseAdapter {
		Context context;
		ArrayList<AMessge> list;

		public MsgAdapter(Context context, ArrayList<AMessge> list) {
			this.list = list;
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list == null ? 0 : list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			HouderView houderView;
			if (convertView == null) {
				houderView = new HouderView();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_systemmsg, null);

				houderView.tv_sys_title = (TextView) convertView
						.findViewById(R.id.tv_msg_title);

				houderView.tv_sys_content = (TextView) convertView
						.findViewById(R.id.tv_msg_content);

				convertView.setTag(houderView);
			} else {
				houderView = (HouderView) convertView.getTag();
			}
			// 订单消息
			houderView.tv_sys_title.setText(list.get(position).getSummary());
			houderView.tv_sys_content.setText(list.get(position).getContent());
			return convertView;
		}

		class HouderView {

			private TextView tv_sys_title;// 订单
			private TextView tv_sys_content;
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		showProgressDialog("正在刷新，请稍后", true);
		pageNo = 1;
		fenSiPresenter.systemMessage(uid, msgType, pageNo);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		showProgressDialog("正在加载，请稍后", true);
		pageNo++;
		fenSiPresenter.systemMessage(uid, msgType, pageNo);
	}

	public void stopLoad() {
		msgListview.stopLoadMore();
		msgListview.stopRefresh();
	}

}
